﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using JQuery.WebKit.Base;
using JQuery.WebKit.WebCommon;
using JQuery.WebKit.WebCommon.Attributes;

#region resources
[assembly: WebResource("JQuery.WebKit.JCollapsiblePanel.JCollapsiblePanel.js", "text/javascript")]
[assembly: WebResource("JQuery.WebKit.JCollapsiblePanel.JCollapsiblePanel.min.js", "text/javascript")]
[assembly: WebResource("JQuery.WebKit.JCollapsiblePanel.JCollapsiblePanel.css", "text/css", PerformSubstitution = true)]
[assembly: WebResource("JQuery.WebKit.JCollapsiblePanel.images.expand.jpg", "image/jpeg")]
[assembly: WebResource("JQuery.WebKit.JCollapsiblePanel.images.collapse.jpg", "image/jpeg")]
[assembly: WebResource("JQuery.WebKit.JCollapsiblePanel.images.bg-header.png", "image/png")]
#endregion
namespace JQuery.WebKit
{
    [ParseChildren(typeof(Panel), DefaultProperty = "Content", ChildrenAsProperties = true)]
    public class JCollapsiblePanel : WebControlBase, INamingContainer
    {
        public bool ShowExpanded { get; set; }
        
        [PersistenceMode(PersistenceMode.InnerProperty)]
        public Panel Header { get; private set; }

        [PersistenceMode(PersistenceMode.InnerProperty)]
        public Panel Content { get; private set; }

        public override ControlCollection Controls
        {
            get
            {
                return Content.Controls;
            }
        }

        public JCollapsiblePanel()
            : base("div")
        {
            Header = new Panel();
            Content = new Panel();
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
        }

        protected override void AddAttributesToRender(HtmlTextWriter writer)
        {
            this.CssClass = "containerPanel";
            this.Header.CssClass = "collapsePanelHeader";
            this.Content.CssClass = "content";

            if (!ShowExpanded)
            {
                this.Content.Style.Add(HtmlTextWriterStyle.Display, "none");
            }

            base.AddAttributesToRender(writer);
        }

        protected override void CreateChildControls()
        {
            base.Controls.Add(Header);
            base.Controls.Add(Content);
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.RegisterJQueryResource(Globals.ResourcePaths.JQueryLibraries.JQueryPlugins.JQueryCookie);
            base.RegisterJQueryResource("JQuery.WebKit.JCollapsiblePanel.JCollapsiblePanel.js");
            base.RegisterCSSResource("JQuery.WebKit.JCollapsiblePanel.JCollapsiblePanel.css");

            string script = string.Format("$('#{0}').jCollapsiblePanel({1});", this.ClientID, this.GetJSProperties("options"));
            Page.Resources.Add(new WebScriptResource(typeof(JCollapsiblePanel), this.ClientID, ResourceType.Javascript, script, true, LoadEvent.DocumentReady));

            base.OnPreRender(e);
        }

        protected override void RenderContents(HtmlTextWriter writer)
        {
            Header.RenderBeginTag(writer);
            writer.Write("<div class='headerContent'>");
            foreach (Control ctrl in Header.Controls)
            {
                ctrl.RenderControl(writer);
            }
            writer.Write("</div>");
            writer.Write("<div class='arrow {0}'></div>", ShowExpanded ? "arrowClose" : "arrowExpand");
            Header.RenderEndTag(writer);
            Content.RenderControl(writer);
        }
    }
}
